#定性数据的编码
from sklearn.preprocessing import OneHotEncoder
enc= OneHotEncoder()
X = [['male', 'from US', 'uses Safari'], ['female', 'from Europe', 'uses Firefox'],['female','from Asia', 'uses Edge']]
X_encoded=enc.fit_transform(X)
print('样本的编码为:',X_encoded.toarray()) #输出样本数据的编码
print('特征的取值为:',enc.categories_) #输出特征的取值
print('特征名称为:', enc.get_feature_names_out()) #输出特征的名称
print('两个新样本数据为:',[['female','from US','uses Safari'],
['male', 'from Europe', 'uses Edge']])
print('两个新样本数据的编码为:',enc.transform([['female','from US','uses Safari'],['male', 'from Europe', 'uses Edge']]).toarray())
#输出两个新样本数据的编码